#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <io.h>
using  namespace std;

//每个政策都作为一个单独文件
//文件读入，使用string存储文件，存储在可变的数组vecter中
//每一个政策的指标使用复合数据结构存储，detection(序号，二维数组存储指标，指标长度，指标总分数)
//对于每一个政策进行特征的分数计算
//输出每一个政策对应的的得分情况，写入到文件中

vector<string> policy;//存储政策文件的vector数组，每一个policy单独存放一个政策
vector<string> final_path;//存储每一个政策文件的绝对地址
double score[8];//八个分量的数值
double all_score;//总和

void getFilesList(string path, string exd, vector<string>& files)//获取文件夹中每一个文件名
{
    //文件句柄
    long hFile = 0;
    //文件信息
    struct _finddata_t fileinfo;
    string pathName, exdName;

    if (0 != strcmp(exd.c_str(), ""))
    {
        exdName = "\\*." + exd;
    }
    else
    {
        exdName = "\\*";
    }

    if ((hFile = _findfirst(pathName.assign(path).append(exdName).c_str(), &fileinfo)) != -1)
    {
        do
        {   
            cout<<hFile<<endl;
            cout<<fileinfo.name<<endl;
            if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                //files.push_back(pathName.assign(path).append("\\").append(fileinfo.name)); // 要得到绝对目录使用该语句
                files.push_back(fileinfo.name); // 只要得到文件名字使用该语句
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
}

vector<string> getFinal_path(){//根据文件名，获取每一个文件可以访问的绝对地址
    //获取该路径下的所有txt文件名称,存入数组files中
    vector<string> files;
    vector<string> final_file_path;
    getFilesList("D:\\C++ project\\c++\\data", "txt", files);
    /*已获取到文件名列表，保存在files中
    for (vector<string>::iterator iter = files.begin(); iter != files.end(); iter++){
		cout << *iter<< endl;
	}*/
    string prefix = "D:\\C++ project\\c++\\data";//文件路径前缀
    for (auto file : files) {//遍历每个文件名，读取其内容，依次追加到all.txt文件中
        string str;
        string path1="D:\\";//根据政策文件绝对地址修改即可
        string fileName = path1+"\\C++ project"+"\\"+"\\"+"c++\\"+"\\data\\"+"\\"+ file;//文件路径前缀+文件名称
        //cout<<fileName<<endl;//每个文件的绝对地址
        final_file_path.push_back(fileName);
    }
    /*cout<<"输入文件路径结果"<<endl;
    for (vector<string>::iterator iter = final_file_path.begin(); iter != final_file_path.end(); iter++)
	{
		cout << *iter<< endl;
	}*/
	return final_file_path;
}

bool Findscore(string data,string find_data){//判断是否含有目标字符串
    int index;
    index=data.find(find_data);
    if(index!=-1){
        return true;
    }
    else{
        return false;
    }
}
void inputFile(string &policy,string filename){//读取文件，将文件转化成data序列
    string temp;
    ifstream infile;
    infile.open(filename,ios::in);
    cout<<filename<<"-----"<<endl;
    if(!infile.is_open()){
        cout<<"未成功打开文件"<<endl;
    }
    while(getline(infile,temp)){
        policy+=temp;
    }
    infile.close();
}

double policyNature(string policy){
    string weight[4]={"预测","监管","建议","引导"};
    int weight_int[4]={0};
    int count=0;
    if(Findscore(policy,"预计")||Findscore(policy,"目标")||Findscore(policy,"展望")||Findscore(policy,"远期")||Findscore(policy,"中长期")||Findscore(policy,"短期")||Findscore(policy,"预期")||Findscore(policy,"推测")){
        weight_int[0]=1;
    }
    if(Findscore(policy,"跟踪")||Findscore(policy,"督促")||Findscore(policy,"监管")||Findscore(policy,"监督")||Findscore(policy,"管理")||Findscore(policy,"检测")||Findscore(policy,"监控")||Findscore(policy,"预警")||Findscore(policy,"风险")){
        weight_int[1]=1;
    }
    if(Findscore(policy,"开展")||Findscore(policy,"加强")||Findscore(policy,"增强")||Findscore(policy,"建立")||Findscore(policy,"强化")||Findscore(policy,"协调")||Findscore(policy,"制定")||Findscore(policy,"研发")||Findscore(policy,"开发")||Findscore(policy,"建设")){
        weight_int[2]=1;
    }
    if(Findscore(policy,"推动")||Findscore(policy,"引导")||Findscore(policy,"鼓励")||Findscore(policy,"支持")||Findscore(policy,"引领")||Findscore(policy,"规划")||Findscore(policy,"形成")||Findscore(policy,"布局")||Findscore(policy,"构建")||Findscore(policy,"方向")){
        weight_int[3]=1;
    }

    double avg=0.25;//该属性的每个分量分值
    cout<<"政治性质的二级变量赋分为       ";
    /*for(int i=0;i<4;i++){
        cout<<weight[i]<<" ";
    }
    cout<<endl;*/
    for(int i=0;i<4;i++){
        cout<<weight_int[i];
    }
    //cout<<endl;
    for(int i=0;i<4;i++){
        if(weight_int[i]==1){
            count+=1;
        }
    } 
    cout<<"       平均值为    "<<avg*count<<endl; 
    //cout<<"-------------------------------"<<endl;  
    return avg*count;//返回该属性分值
}

double policyAging(string policy){
    string weight[3]={"短期","中期","长期"};
    int weight_int[3]={0};
    if(Findscore(policy,"2050")||Findscore(policy,"长期")||Findscore(policy,"中长期")||Findscore(policy,"20年")){
        weight_int[2]=1;
    }else if(Findscore(policy,"2030")||Findscore(policy,"2035")||Findscore(policy,"中期")||Findscore(policy,"10年")){
        weight_int[1]=1;
    }else if(Findscore(policy,"2025")||Findscore(policy,"短期")||Findscore(policy,"5年")){
        weight_int[0]=1;
    }
    double avg=0.333;//该属性的每个分量分值
    cout<<"政策时效一级变量的二级变量赋分为       ";
    for(int i=0;i<3;i++){
        cout<<weight_int[i];
   }
   cout<<"       平均值为    "<<avg<<endl;  
    return avg;//返回该属性分值
}
double policyFiled(string policy){
    string weight[4]={"经济","社会","科技","环境"};
    int weight_int[4]={0};
    int count=0;
    if(Findscore(policy,"金融")||Findscore(policy,"资金")||Findscore(policy,"财政")||Findscore(policy,"税")||Findscore(policy,"投资")||Findscore(policy,"商业")||Findscore(policy,"融资")||Findscore(policy,"经济")){
        weight_int[0]=1;
    }
    if(Findscore(policy,"制度")||Findscore(policy,"人才")||Findscore(policy,"高校")||Findscore(policy,"社会")||Findscore(policy,"宣传")||Findscore(policy,"学校")){
        weight_int[1]=1;
    }
    if(Findscore(policy,"技术")||Findscore(policy,"装备")||Findscore(policy,"研发")||Findscore(policy,"创新")||Findscore(policy,"科技")||Findscore(policy,"科研")||Findscore(policy,"工艺")){
        weight_int[2]=1;
    }
    if(Findscore(policy,"能源")||Findscore(policy,"低碳")||Findscore(policy,"环保")||Findscore(policy,"环境")||Findscore(policy,"气候")||Findscore(policy,"绿色")||Findscore(policy,"节能")){
        weight_int[3]=1;
    }

    double avg=0.25;//该属性的每个分量分值
    cout<<"政策领域一级变量的二级变量赋分为       ";
    /*for(int i=0;i<4;i++){
        cout<<weight[i]<<" ";
    }
    cout<<endl;*/
    for(int i=0;i<4;i++){
        cout<<weight_int[i];
    }
    //cout<<endl;
    for(int i=0;i<4;i++){
        if(weight_int[i]==1){
            count+=1;
        }
    } 
    cout<<"       平均值为    "<<avg*count<<endl; 
    //cout<<"-------------------------------"<<endl;    
    return avg*count;//返回该属性分值
}

double policyHydrogenApplication(string policy){
    string weight[5]={"交通","电力","钢铁","建材","煤炭"};
    int weight_int[5]={0};
    int count=0;
    if(Findscore(policy,"车")||Findscore(policy,"汽车")||Findscore(policy,"新能源汽车")||Findscore(policy,"交通")){
        weight_int[0]=1;
    }
    if(Findscore(policy,"风电")||Findscore(policy,"光伏")||Findscore(policy,"电力")||Findscore(policy,"发电")){
        weight_int[1]=1;
    }
    if(Findscore(policy,"钢铁")||Findscore(policy,"炼铁")||Findscore(policy,"冶金")||Findscore(policy,"钢材")){
        weight_int[2]=1;
    }
    if(Findscore(policy,"建材")||Findscore(policy,"建筑")||Findscore(policy,"土木")){
        weight_int[3]=1;
    }
    if(Findscore(policy,"煤")||Findscore(policy,"煤炭")||Findscore(policy,"开采")||Findscore(policy,"焦炭")||Findscore(policy,"焦炉气")){
        weight_int[4]=1;
    }

    double avg=0.2;//该属性的每个分量分值
    cout<<"政策应用领域一级变量的二级变量赋分为       ";
    /*for(int i=0;i<5;i++){
        cout<<weight[i]<<" ";
    }
    cout<<endl;*/
    for(int i=0;i<5;i++){
        cout<<weight_int[i];
    }
    //cout<<endl;
    for(int i=0;i<5;i++){
        if(weight_int[i]==1){
            count+=1;
        }
    } 
    cout<<"       平均值为    "<<avg*count<<endl; 
    //cout<<"-------------------------------"<<endl;     
    return avg*count;//返回该属性分值
}
double policyContent(string policy){
    string weight[12]={"核心技术","基础建设","示范工程","税收优惠","财政补贴","产业标准","人才培养","国际合作","平台建设","宣传引导","安全管理","立法保障"};
    int weight_int[12]={0};
    int count=0;
    if(Findscore(policy,"技术")||Findscore(policy,"研发")||Findscore(policy,"创新")){
        weight_int[0]=1;
    }
    if(Findscore(policy,"基础建设")||Findscore(policy,"基础工程")||Findscore(policy,"基础设施")){
        weight_int[1]=1;
    }
    if(Findscore(policy,"示范")||Findscore(policy,"试点")){
        weight_int[2]=1;
    }
    if(Findscore(policy,"税")||Findscore(policy,"税收优惠")){
        weight_int[3]=1;
    }
    if(Findscore(policy,"补贴")||Findscore(policy,"贷款")){
        weight_int[4]=1;
    }
    if(Findscore(policy,"标准")||Findscore(policy,"贷款")){
        weight_int[5]=1;
    }
    if(Findscore(policy,"人才")||Findscore(policy,"高校")){
        weight_int[6]=1;
    }
    if(Findscore(policy,"国际")||Findscore(policy,"国际交流")){
        weight_int[7]=1;
    }
    if(Findscore(policy,"平台")){
        weight_int[8]=1;
    }
    if(Findscore(policy,"宣传")||Findscore(policy,"普及")){
        weight_int[9]=1;
    }
    if(Findscore(policy,"安全")||Findscore(policy,"风险")){
        weight_int[10]=1;
    }
    if(Findscore(policy,"立法")||Findscore(policy,"法规")){
        weight_int[11]=1;
    }

    double avg=0.0833;//该属性的每个分量分值
    cout<<"政策内容一级变量的二级变量赋分为       ";
    /*for(int i=0;i<12;i++){
        cout<<weight[i]<<" ";
    }
    cout<<endl;*/
    for(int i=0;i<12;i++){
        cout<<weight_int[i];
    }
    //cout<<endl;
    for(int i=0;i<12;i++){
        if(weight_int[i]==1){
            count+=1;
        }
    }   
    cout<<"       平均值为    "<<avg*count<<endl;  
    //cout<<"-------------------------------"<<endl;  
    return avg*count;//返回该属性分值
}


double policyFunction(string policy){
    string weight[4]={"技术创新","节能减排","规范引导","能源替代"};
    int weight_int[4]={0};
    int count=0;
    if(Findscore(policy,"技术")||Findscore(policy,"研发")||Findscore(policy,"创新")||Findscore(policy,"装备")||Findscore(policy,"科技")||Findscore(policy,"科研")||Findscore(policy,"工艺")){
        weight_int[0]=1;
    }
    if(Findscore(policy,"能源")||Findscore(policy,"低碳")||Findscore(policy,"环保")||Findscore(policy,"环境")||Findscore(policy,"气候")||Findscore(policy,"绿色")||Findscore(policy,"节能")){
        weight_int[1]=1;
    }
    if(Findscore(policy,"引导")||Findscore(policy,"鼓励")||Findscore(policy,"支持")||Findscore(policy,"引领")||Findscore(policy,"规划")||Findscore(policy,"布局")||Findscore(policy,"方向")||Findscore(policy,"规范")){
        weight_int[2]=1;
    }
    if(Findscore(policy,"新能源")||Findscore(policy,"化石能源")||Findscore(policy,"新型能源")||Findscore(policy,"可再生能源")){
        weight_int[3]=1;
    }

    double avg=0.25;//该属性的每个分量分值
    cout<<"政策功能一级变量的二级变量赋分为       ";
    /*for(int i=0;i<4;i++){
        cout<<weight[i]<<" ";
    }
    cout<<endl;*/
    for(int i=0;i<4;i++){
        cout<<weight_int[i];
    }
    //cout<<endl;
    for(int i=0;i<4;i++){
        if(weight_int[i]==1){
            count+=1;
        }
    } 
    cout<<"       平均值为    "<<avg*count<<endl;  
    //cout<<"-------------------------------"<<endl;    
    return avg*count;//返回该属性分值
}
double policyCCU(string policy){
    string weight[2]={"是","否"};
    int weight_int[2]={0};
    int count=0;
    if(Findscore(policy,"二氧化碳捕集和利用")||Findscore(policy,"碳捕集")||Findscore(policy,"碳利用")||Findscore(policy,"CCU")){
        weight_int[0]=1;
    }else{
        weight_int[1]=1;
    }

    double avg=0.5;//该属性的每个分量分值
    cout<<"是否与CCU协同发展一级变量的二级变量赋分为       ";
    /*for(int i=0;i<2;i++){
        cout<<weight[i]<<" ";
    }
    cout<<endl;*/
    for(int i=0;i<2;i++){
        cout<<weight_int[i];
    }
    //cout<<endl;
    for(int i=0;i<2;i++){
        if(weight_int[i]==1){
            count+=1;
        }
    } 
    cout<<"       平均值为    "<<avg*count<<endl;  
    //cout<<"-------------------------------"<<endl;    
    return avg*count;//返回该属性分值
}
double policyObject(string policy){
    string weight[5]={"政府","企业","金融机构","社会公众","其他部门"};
    int weight_int[5]={0};
    int count=0;
    if(Findscore(policy,"政府")){
        weight_int[0]=1;
    }
    if(Findscore(policy,"企业")){
        weight_int[1]=1;
    }
    if(Findscore(policy,"金融机构")||Findscore(policy,"银行")){
        weight_int[2]=1;
    }
    if(Findscore(policy,"群众")||Findscore(policy,"公众")||Findscore(policy,"公民")||Findscore(policy,"人民")){
        weight_int[3]=1;
    }
    if(Findscore(policy,"高校")||Findscore(policy,"院校")||Findscore(policy,"创新中心")||Findscore(policy,"研究中心")||Findscore(policy,"实验室")||Findscore(policy,"研究平台")||Findscore(policy,"科研院所")){
        weight_int[4]=1;
    }

    double avg=0.2;//该属性的每个分量分值
    cout<<"政策客体一级变量的二级变量赋分为       ";
    /*for(int i=0;i<5;i++){
        cout<<weight[i]<<" ";
    }
    cout<<endl;*/
    for(int i=0;i<5;i++){
        cout<<weight_int[i];
    }
    //cout<<endl;
    for(int i=0;i<5;i++){
        if(weight_int[i]==1){
            count+=1;
        }
    }  
    cout<<"       平均值为    "<<avg*count<<endl;
    //cout<<"-------------------------------"<<endl;    
    return avg*count;//返回该属性分值
}

double policyActionRange(string policy){
    string weight[4]={"制氢","储运","加氢","用氢"};
    int weight_int[4]={0};
    int count=0;
    if(Findscore(policy,"制")||Findscore(policy,"储")||Findscore(policy,"输")||Findscore(policy,"用")){
        weight_int[0]=1;
        weight_int[1]=1;
        weight_int[3]=1;
    }
    if(Findscore(policy,"运氢")){
        weight_int[1]=1;
    }
    if(Findscore(policy,"加氢")){
        weight_int[2]=1;
    }

    double avg=0.25;//该属性的每个分量分值
    cout<<"政策作用范围一级变量的二级变量赋分为       ";
    /*for(int i=0;i<4;i++){
        cout<<weight[i]<<" ";
    }
    cout<<endl;*/
    for(int i=0;i<4;i++){
        cout<<weight_int[i];
    }
    //cout<<endl;
    for(int i=0;i<4;i++){
        if(weight_int[i]==1){
            count+=1;
        }
    }
    cout<<"       平均值为    "<<avg*count<<endl; 
    //cout<<"-------------------------------"<<endl;  
    return avg*count;//返回该属性分值
}
void inputAllfiles(){//根据所有路径，将所有文件内容存入policy数组
    string policystring;//存储单个政策文件
    string final_path_string;//存储单个绝对路径
    for (vector<string>::iterator iter = final_path.begin(); iter != final_path.end(); iter++){
        policystring="";//每一个都清空
		final_path_string=*iter;
        inputFile(policystring,final_path_string);
        policy.push_back(policystring);
	}
}
void PMGassess(){
    string policystring;
    int count=0;
    ofstream oFile; 
	oFile.open("D:\\C++ project\\c++\\secore.csv", ios::out | ios::trunc);    //输出一个需要的excel 文件
    oFile <<"policyCount"<<","<<"policyNature"<<","<<"policyAging"<<","<<"policyFiled"<<","<<"policyHydrogenApplication"<<","<<"policyContent"<< "," <<"policyFunction"<<","<<"policyCCU"<<","<<"policyObject"<<","<<"policyActionRange"<<","<<"PMG"<< endl; 
    for (vector<string>::iterator iter = policy.begin(); iter != policy.end(); iter++){
		policystring=*iter;
        cout<<"第"<<++count<<"条政策内容为："<<endl;
        cout<<policystring<<endl;
        cout<<"该政策各个标量赋值为："<<endl;
        double r1=policyNature(policystring);
        double r9=policyAging(policystring);
        double r2=policyFiled(policystring);
        double r3=policyHydrogenApplication(policystring);
        double r4=policyContent(policystring);
        double r5=policyFunction(policystring);
        double r6=policyCCU(policystring);
        double r7=policyObject(policystring);
        double r8=policyActionRange(policystring);
        double result=r1+r2+r3+r4+r5+r6+r7+r8;
        cout<<"PMG总分为："<<result<<endl;
	    oFile <<count<<","<<r1<<","<<r9<<","<<r2<<","<<r3<<","<<r4<<","<<r5<< "," <<r6<<","<<r7<<","<<r8<<","<<result<< endl; 
        cout<<"------------------分割线---------------------"<<endl;
	}
    oFile.close();
}
void allProcess(){//完整操作集合
    final_path=getFinal_path();//首先获取所有政策的绝对地址
    inputAllfiles();//第二步，根据所有绝对地址，读入所有政策信息
    PMGassess();//最后，对每一个政策都进行PMG值的分析
}
int main(){
    allProcess();//全部操作
    return 0;
}